package swing.swing04;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JProgressTest2 {

    JFrame jf=new JFrame("测试进度条");

    JCheckBox indeterminate=new JCheckBox("不确定进度");
    JCheckBox noBorder=new JCheckBox("不绘制边框");

    //创建进度条
    JProgressBar bar=new JProgressBar(JProgressBar.HORIZONTAL,0,100);

    public void init(){

        indeterminate.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                // 获取一下indeterminate复选框有没有选中
                boolean selected = indeterminate.isSelected();

                // 是否设置成不确定边框
                bar.setIndeterminate(selected);
                // 是否设置百分比
                bar.setStringPainted(!selected);
            }
        });

        noBorder.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                // 获取一下noBorder复选框有没有选中
                boolean selected = noBorder.isSelected();
                bar.setBorderPainted(!selected);
            }
        });

        Box vBox=Box.createVerticalBox();

        vBox.add(indeterminate);
        vBox.add(noBorder);

        //设置进度条的属性
        bar.setStringPainted(true);
        bar.setBorderPainted(true);

        // 把当前窗口的布局方式修改为FlowLayout
        jf.setLayout(new FlowLayout());
        jf.add(vBox);
        jf.add(bar);

//        //通过循环模拟修改进度条的进度  这段代码必须放在下面
//        for(int i=0;i<=100;i++){
//            //修改百分比
//            bar.setValue(i);
//            try {
//                Thread.sleep(500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }


        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);

        // 开启子线程模拟耗时操作

        SimulaterActivity simulaterActivity= new SimulaterActivity(bar.getMaximum()); // bar.getMaximum() =>100
        new Thread(simulaterActivity).start(); //开启子线程 执行SimulaterActivity的run方法

        // 设置一个定时任务
        Timer timer=new Timer(200,new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // 读取线程任务对象的当前完成量 设置给进度条
                int current = simulaterActivity.getCurrent();
                bar.setValue(current);
            }
        });

        timer.start();

        // 监听进度条的任务变化
        bar.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e){
                int value = bar.getValue();
                if(value==simulaterActivity.getAmount()){
                    timer.stop();
                }
            }
        });

    }

    private class SimulaterActivity implements Runnable{

        // 记录任务总量 外界传进来
        private int amount;

        // 记录当前任务的完成量
        private int current;

        public SimulaterActivity(int amount) {
            this.amount = amount;
        }

        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }

        public int getCurrent() {
            return current;
        }

        public void setCurrent(int current) {
            this.current = current;
        }

        @Override
        public void run(){
            // 子线程任务的模拟耗时操作
            while(current<amount){
                try {
                    Thread.currentThread().sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                current++;

            }
        }
    }


    public static void main(String[] args){
        JProgressTest2 jpt=new JProgressTest2();
        jpt.init();
    }
}
